﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace Split
{




    static class Function
    {
        
        
        public static void Split(ref List<Functions> CodeFunctions, List<Expression> CodeTokens)
        {
            //////////////////////////
            //コードの関数分割
            int FuncCounter = 0;
            CodeFunctions.Add(new Functions());


            int TokenCount = 0;//TokenListのIndex
            bool NameSign = false;//名前か
            bool MainSign = false;//中身か

            foreach (Expression e in CodeTokens) //CodeTokensのループ
            {


                string[] TokenList = e.TokenList.ToArray();
                foreach (string Token in TokenList)//文ループ
                {
                    CodeFunctions[FuncCounter].Expression.Add(new Expression());
                    Console.WriteLine("[Debug::Split::Function::TokenCount]{0}", TokenCount);
                    Console.WriteLine("[Debug::Split::Function::Token]{0}", Token);
                    if (Token == "@")
                    {
                        if (NameSign == true)
                        {
                            NameSign = false;//ここから先は名前ではない
                            continue;
                        }
                        else if (NameSign == false)
                        {
                            NameSign = true;//引数である
                            continue;
                        }
                    }

                    else if (Token == "[")
                    {
                        Console.WriteLine("[Debug::Split::Function::StartToken]OK");
                        MainSign = true;
                        continue;
                    }
                    else if (Token == "]")
                    {
                        Console.WriteLine("[Debug::Split::Function::EndToken]OK");
                        MainSign = false;
                        FuncCounter++;
                        CodeFunctions.Add(new Functions());
                        continue;
                        
                    }

                    else
                    {
                        if (NameSign == true)
                        {
                            CodeFunctions[FuncCounter].Name += Token;
                            continue;
                        }
                        else if(MainSign == true)//中身なら
                        {
                            Console.WriteLine("{0},{1}", CodeFunctions[FuncCounter].Expression.Count,TokenCount);
                            CodeFunctions[FuncCounter].Expression[TokenCount].TokenList.Add(Token);
                        }
                    }
                    TokenCount++;
                }

            }
        }


    }




}